<template>
  <Wmodule title="土壤墒情" class="m2">
    <div class="turDeatil">
      <div class="tur-item" v-for="(item, index) in list" :key="index">
        <div class="tur-item-left">
          <img :src="item.icon" alt="" class="icon" />
        </div>
        <div class="tur-item-right">
          <div class="tur-item-right-value">
            {{ item.value || '--' }} {{ item.unit }}
          </div>
          <div class="tur-item-right-name">{{ item.name }}</div>
        </div>
      </div>
    </div>
  </Wmodule>
</template>
<script setup>
import { ref, watch, toRaw } from 'vue'
import Wmodule from '@/components/wmodule/index.vue'
import imgUrl13 from '@/assets/img/13.png'
import imgUrl14 from '@/assets/img/14.png'
import imgUrl15 from '@/assets/img/15.png'
import imgUrl16 from '@/assets/img/16.png'
import imgUrl17 from '@/assets/img/17.png'
import imgUrl18 from '@/assets/img/18.png'
import imgUrl30 from '@/assets/img/30.png'

const props = defineProps({
  data: {
    type: Array,
    default: () => []
  }
})
const options = [
  {
    icon: imgUrl13,
    name: '温度',
    value: 0,
    unit: 'C°',
    deviceItemName: '土壤温度'
  },
  {
    icon: imgUrl14,
    name: '湿度',
    value: 0,
    unit: '%',
    deviceItemName: '土壤湿度'
  },
  {
    icon: imgUrl15,
    name: '电导率',
    value: 0,
    unit: '%',
    deviceItemName: '电导率'
  },
  {
    icon: imgUrl16,
    name: 'PH值',
    value: 0,
    deviceItemName: '土壤PH值'
  },
  {
    icon: imgUrl17,
    name: '盐度',
    value: 0,
    deviceItemName: '土壤盐度'
  },
  {
    icon: imgUrl18,
    name: '氮含量',
    value: 0,
    deviceItemName: '土壤含氮量'
  },
  // {
  //   icon: imgUrl18,
  //   name: '张力',
  //   value: 0,
  //   deviceItemName: '土壤张力'
  // },
  {
    icon: imgUrl17,
    name: '磷含量',
    value: 0,
    deviceItemName: '土壤含磷量'
  },
  {
    icon: imgUrl30,
    name: '钾含量',
    value: 0,
    deviceItemName: '土壤含钾量'
  }
]
const list = ref(options)

watch(
  () => props.data,
  (newVal) => {
    const _newVal = toRaw(newVal)
    if (_newVal.length > 0) {
      const _list = options.map((item) => {
        const row = _newVal.find(
          (item2) => item2.deviceItemName === item.deviceItemName
        )
        if (row) {
          const _metData = row.soilData || {}
          if (item.name === '湿度') {
            return {
              ...item,
              value: _metData.humValueStr,
              unit: _metData.humUnit
            }
          }
          return {
            ...item,
            value: _metData.temValueStr,
            unit: ['PH值', '盐度'].includes(item.name) ? '' : _metData.temUnit
          }
        } else {
          return item
        }
      })
      list.value = _list
    }
  }
)
</script>
<style lang="scss" scoped>
.m2 {
  height: 100%;
}
.turDeatil {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-row: 18px;
  padding: 20px;
  box-sizing: border-box;
  .tur-item {
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
    .tur-item-left {
      display: flex;
      justify-content: center;
      align-items: center;
      .icon {
        width: 62px;
        height: 62px;
        display: block;
      }
    }
    .tur-item-right {
      display: flex;
      justify-content: center;
      align-items: flex-start;
      flex-direction: column;
      .tur-item-right-name {
        font-family: Microsoft YaHei;
        font-weight: bold;
        font-size: 14px;
        color: #e7ffec;
        line-height: 22px;
      }
      .tur-item-right-value {
        font-family: Source Han Sans CN;
        font-weight: 400;
        font-size: 14px;
        color: #e7ffec;
        line-height: 22px;
      }
    }
  }
}
</style>
